/*
 * =====================================================================================
 *
 *       Filename:  cl_idx_table.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  04/02/2011 11:48:09 PM
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Chen Ken (Ken), zlchen.ken@gmail.com
 *        Company:  EMC
 *
 * =====================================================================================
 */

#ifndef _CL_IDX_TABLE_H_
#define _CL_IDX_TABLE_H_

#include <algorithm>
#include <map>

namespace neo
{
template<typename T>
struct CLCompareDeref
{
public:
  bool operator()(const T& lhs, const T& rhs) const
	{
		return *lhs < *rhs;
	}
};

template<typename T, typename U>
struct CLPair2nd
{
public:
  const U& operator()(const std::pair<T, U>& a) const
	{
		return a.second;
	}
};

template<typename IterIn, typename IterOut>
void clSortIdxTbl(IterIn first, IterIn last, IterOut out)
{
	std::multimap<IterIn, int, CLCompareDeref<IterIn> > v;
	for(int i = 0; first != last; ++i, ++first)
	{
		v.insert(std::make_pair(first, i));
	}

	std::transform(v.begin(), v.end(), out, CLPair2nd<IterIn const, int>());
}

} // neo

#endif 

